package com.wzw.springboot.principle.aware;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author Wangzhiwen
 */
@Component
public class CustomApplicationContext implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    /**
     * 实现 对应的Aware 接口，拿到在容器内对应的对象
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        CustomApplicationContext.applicationContext = applicationContext;
        System.out.println("CustomApplicationContext implement ApplicationContextAware interface 拿到 applicationContext 对象" + applicationContext);
    }

    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }

    public static <T> T getBean(String name, Class<T> clazz) {
        return applicationContext.getBean(name, clazz);
    }
}
